﻿using Abp.AutoMapper;
using Batman.FormGenerator.Domain;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Batman.FormGenerator.Controllers
{
    [AutoMap(typeof(Form))]
    public class CreateFormInput
    {
        [Required(ErrorMessage = "表单标题不能空，请输入表单标题")]
        public string Title { get; set; }

        public string Description { get; set; }

        [Required(ErrorMessage = "至少需要一个表单项")]
        public List<FormField> Fields { get; set; }

        public string FieldsData
        {
            get
            {
                if (Fields == null)
                {
                    return JsonConvert.SerializeObject(new List<FormField>());
                }
                return JsonConvert.SerializeObject(Fields);
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    Fields = new List<FormField>();
                }
                else
                {
                    Fields = JsonConvert.DeserializeObject<List<FormField>>(value);
                }
            }
        }
    }
}
